package com.example.administrator.fragarmentdemotest_a.fragarment_demo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.fragarmentdemotest_a.R;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.frag.MainFragment_A;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.frag.MainFragment_B;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.frag.MainFragment_C;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private FrameLayout fl;
    private RadioGroup rg;
    private RadioButton rba, rbb, rbc;
    private Fragment[] frags;
    private Fragment[] okfrags = new Fragment[3];
    private DrawerLayout drawerLayout;
    private TextView tvxa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        rba = findViewById(R.id.rba);
        rbb = findViewById(R.id.rbb);
        rbc = findViewById(R.id.rbc);
        rg = findViewById(R.id.rg);
        tvxa = findViewById(R.id.tvxa);
        drawerLayout = findViewById(R.id.drawerlayoutone);
        drawerLayout.openDrawer(tvxa);
        rg.setOnCheckedChangeListener(this);
        frags = new Fragment[]{new MainFragment_A(), new MainFragment_B(), new MainFragment_C()};
        rg.check(R.id.rbb);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rba: {
                changeCheckedFragment(0);
                rba.setTextSize(33);
                rbb.setTextSize(21);
                rbc.setTextSize(21);
                break;
            }
            case R.id.rbb: {
                changeCheckedFragment(1);
                rba.setTextSize(21);
                rbb.setTextSize(33);
                rbc.setTextSize(21);
                break;
            }
            case R.id.rbc: {
                changeCheckedFragment(2);
                rba.setTextSize(21);
                rbb.setTextSize(21);
                rbc.setTextSize(33);
                break;
            }
        }

    }

    private void changeCheckedFragment(int i) {
        FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
        for (Fragment frag : frags) {
            if (frag != null)
                fts.hide(frag);
        }
        if (okfrags[i] == null) {
            okfrags[i] = frags[i];
            fts.add(R.id.fl, okfrags[i]);
            fts.show(okfrags[i]);
        } else {

            fts.show(okfrags[i]);

        }
        fts.commit();
    }


}
